import { useState } from "react";
import { Button, message, Form, Input, Space } from "antd";
import { useModel } from "umi";

const EditApplicationTypeVersionDescriptionForm = ({ version, reloadData }) => {
    const { remoteData } = useModel("global");
    const { onCloseGlobalModal } = useModel("global.modal");
    const { remoteOrganizationInfo } = useModel("organization.info");

    const [submitLoading, setSubmitLoading] = useState(false);
    const [form] = Form.useForm();

    const onFinish = async (values) => {
        setSubmitLoading(true);
        let res = await remoteData({
            object: "bnt-application-resource-center-service",
            serviceName: "/developer/application/updateVersionDescription",
            params: {
                uniformSocialCreditCode: remoteOrganizationInfo.uniformSocialCreditCode,
                apvPubtime: version.apvPubtime,
                ...values,
            },
        });
        if (res) {
            message.success("成功更新应用客户端信息");
            form.resetFields();
            reloadData();
            onCloseGlobalModal();
        }
        setSubmitLoading(false);
    };

    const apvDescValidater = (rule, value) => {
        if (value && validates.textArea(value)) {
            return Promise.resolve();
        } else {
            return Promise.reject("版本说明含有非法字符，请检查");
        }
    };

    return (
        <div style={{ padding: "0 30px 0 30px" }}>
            <Form form={form} onFinish={onFinish} layout="vertical" initialValues={version}>
                <Form.Item label="版本说明（更新说明）" name="apvDesc" rules={[{ required: true, validator: apvDescValidater }]}>
                    <Input.TextArea showCount autoSize={{ minRows: 4, maxRows: 16 }} />
                </Form.Item>

                <Form.Item style={{ textAlign: "center" }}>
                    <Space>
                        <Button loading={submitLoading} type="primary" htmlType="submit">
                            保存版本说明信息
                        </Button>
                        <Button loading={submitLoading} htmlType="button" onClick={() => form.resetFields()}>
                            重置
                        </Button>
                    </Space>
                </Form.Item>
            </Form>
        </div>
    );
};

export default EditApplicationTypeVersionDescriptionForm;
